﻿#region LGPL License

/*
OpenSearchNet
Copyright (c) 2014, Brad Hards <bradh@frogmouth.net>, All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library.
*/

#endregion LGPL License

using System;
using System.Globalization;

namespace OpenSearch.Osdd
{
    /// <summary>
    /// URL from an OpenSearch Description Document
    /// </summary>
    /// <remarks>
    /// <para>
    /// Describes an interface by which a client can make requests for an external resource, such as
    /// search results, search suggestions, or additional description document.
    /// </para>
    /// </remarks>
    [CLSCompliant(true)]
    public class Url
    {
        private readonly int DEFAULT_INTEGER_OFFSET = 1;
        private readonly int DEFAULT_PAGE_OFFSET = 1;
        private int indexOffset;
        private int pageOffset;
        private readonly string DEFAULT_RELATION = "results";
        private string rel;

        internal Url(System.Xml.XmlTextReader reader)
        {
            MimeType = reader["type"];
            Template = new OpenSearch.Template(reader["template"], reader.GetNamespacesInScope(System.Xml.XmlNamespaceScope.ExcludeXml), reader.NamespaceURI);
            indexOffset = DEFAULT_INTEGER_OFFSET;
            if (reader.GetAttribute("indexOffset") != null)
            {
                indexOffset = Int32.Parse(reader.GetAttribute("indexOffset"), CultureInfo.InvariantCulture);
            }
            pageOffset = DEFAULT_PAGE_OFFSET;
            if (reader.GetAttribute("pageOffset") != null)
            {
                pageOffset = Int32.Parse(reader.GetAttribute("pageOffset"), CultureInfo.InvariantCulture);
            }
            rel = DEFAULT_RELATION;
            if (reader.GetAttribute("rel") != null)
            {
                rel = reader.GetAttribute("rel");
            }
        }

        /// <summary>
        /// MIME type associated with this search URL
        /// </summary>
        /// <remarks>
        /// <para>
        /// This is typically <c>application/atom+xml</c> or <c>application/rss+xml</c>, although other types are possible.
        /// </para>
        /// </remarks>
        public string MimeType { get; set; }

        /// <summary>
        /// Index number of the first search result
        /// </summary>
        /// <remarks>
        /// <para>
        /// This provides alternative options for the search result indexing (e.g. for 5 results, whether they are number 1 through 5, or 0 through 4).
        /// The default value is 1.
        /// </para>
        /// </remarks>
        public int IndexOffset
        {
            get { return indexOffset; }
        }

        /// <summary>
        /// Page number of the first search result
        /// </summary>
        /// <remarks>
        /// <para>
        /// This provides alternative options for the search result indexing (e.g. for 4 pages of results, whether the pages are numbered 1 through 4, or 0 through 3).
        /// The default value is 1.
        /// </para>
        /// </remarks>
        public int PageOffset
        {
            get { return pageOffset; }
        }

        /// <summary>
        /// Relation(s) associated with this URL
        /// </summary>
        /// <remarks>
        /// <para>
        /// Space-delimited list of one or more semantically meaningful relations ("rel tokens"). If the client does not understand the semantic meaning of any token,
        /// then the associated URL should be ignored by the client. Tokens can be either a fully qualified token (e.g. "http://example.com/rel#foo") or unqualified
        /// tokens (e.g. "results"). All fully qualified tokens must be a valid URL. The defined unqualified tokens are:
        /// <list type="">
        /// <item><c>results</c> - Represents a request for search results in the specified format.</item>
        /// <item><c>suggestions</c> - Represents a request for search suggestions in the specified format.</item>
        /// <item><c>self</c> - Represents the canonical URL of this description document.</item>
        /// <item><c>collection</c> - Represents a request for a set of resources.</item>
        /// </list>
        /// </para>
        /// <para>
        /// The default value is "results".
        /// </para>
        /// </remarks>
        public string Relation
        {
            get { return rel; }
        }

        /// <summary>
        /// URL template
        /// </summary>
        public OpenSearch.Template Template { get; set; }
    }
}